<?php

namespace polymerize\user\module\user;


use app\common\InstanceTrait;
use app\model\OrderVipcart;
use app\model\OrderVipcartDelivery;
use app\model\User as UserModel;
use app\service\AesService;

class GetUserInfo
{
    use InstanceTrait;

    /**
     * 用户信息
     *
     * @param int $uid
     * @return array
     */
    public function getUserInfo(int $uid): array
    {
        $data = ['user_info' => null, 'vip_info' => null];
        if (!$uid) return $data;

        $userInfo = UserModel::getOne(['id' => $uid], '*');

        if ($userInfo) {
            if (isset($user_info['avatar']) && $userInfo['avatar'] && !strstr($userInfo['avatar'], 'http')) {
                $userInfo['avatar'] = OSS_URL . $userInfo['avatar'];
            }
            $userInfo['birthday'] = (isset($user_info['birthday']) && $user_info['birthday']) ? $userInfo['birthday'] : '';

            //手机号加密
            $userInfo['mobile'] = UserModel::getField(['id' => $uid], 'mobile');
//            $userInfo['mobile_aes'] = $userMobile ? AesService::instance()->setkey(API_AES_KEY)->encrypt($userMobile) : '';

            //用户是否会员
            $where2[]             = ['user_id', '=', $uid];
            $where2[]             = ['status', '=', 1];            //订单支付状态：1 =支付成功2 =支付失败 3 =待支付4 =已退款
            $where2[]             = ['order_status', 'in', [1, 2]];//订单状态：1 =待确认2 =进行中3=已完成4=已取消
            $vipInfo              = OrderVipcart::getOne($where2, 'id,goods_name,order_status,status', 1, 'pay_time desc');
            $userInfo['is_vip']   = 2;//不是会员
            $userInfo['vip_info'] = null;
            if ($vipInfo) {
                $userInfo['is_vip']      = 1;                 //是会员
                $where3[]                = ['status', '=', 1];//配送状态：1 =待配送2 =已配送
                $where3[]                = ['order_vipcart_id', '=', $vipInfo['id']];
                $vipInfo['delivery_day'] = OrderVipcartDelivery::getField($where3, 'delivery_day');//配送日期
                $userInfo['vip_info']    = $vipInfo;
            }
        }

        return $userInfo;
    }
}
